﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FrameWork.Common;
using FrameWork.Entity;
using FrameWork.WebControl;
using FrameWork.BLL;

#foreach($u in $TableInfos)
#beforeall 
#each 
namespace FrameWork.Web.Web.Manager.NormalApp.${u.TabName}Manager
{
    public partial class Manager: System.Web.UI.Page
    {
       /// <summary>
        /// 编辑的id ,editId
        /// </summary>
        public  int eId;

        /// <summary>
        /// 操作类型
        /// </summary>
        private string cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            eId = (int)Utils.GetQueryAndFormString("eid", MethodType.Get, 10,0, DataType.Int);
            cmd = (string)Utils.GetQueryAndFormString("cmd", MethodType.Get, 6, 1, DataType.Str);
            onStart();
            this.TabOptionItem1.Tab_Name = string.Concat(Utils.GetCmdDirections(cmd), this.TabOptionItem1.Tab_Name);
        }
        protected void onStart()
        {
            WebFormHandler hand = new WebFormHandler("$u.TabName", btnSave);
            switch (cmd)
            {
                case "look":
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
 #if($c.TypeName=="bit")
                    hand.AddBind(rbl$c.ColumnName, "$c.ColumnName", true, DataType.$c.TypePostName, 100, 1);
 #else
                     hand.AddBind(lb$c.ColumnName, "$c.ColumnName", true, DataType.$c.TypePostName, 100, 1);
 #end
#else
                    hand.ConditionExpress = "$c.ColumnName=" + eId.ToString();
                    lb$c.ColumnName .Text=eId.ToString();
#end
#end
                    hand.Mode = OperationType.Look;
                    hideTextBox();
                    addDelBtn();
                    addEditBtn();
                    addAddBtn();
                    break;
                case "edit":
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
 #if($c.TypeName=="bit")
                    hand.AddBind(rbl$c.ColumnName, "$c.ColumnName", true, DataType.$c.TypePostName, 100, 1);
 #else
                    hand.AddBind(tb$c.ColumnName, "$c.ColumnName", true, DataType.$c.TypePostName, 100, 1);
 #end
#else
                    hand.ConditionExpress = "$c.ColumnName=" + eId.ToString();
                    hand.Mode = OperationType.Modify;
                    lb$c.ColumnName .Text = eId.ToString();
#end
#end
                    hideLable();
                    addDelBtn();
                    addAddBtn();
                    break;
                case "add":
                    hand.Mode = OperationType.Add;
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
 #if($c.TypeName=="bit")
                    hand.AddBind(rbl$c.ColumnName, "$c.ColumnName", true, DataType.$c.TypePostName, 100, 1);
 #else
                    hand.AddBind(tb$c.ColumnName, "$c.ColumnName", true, DataType.$c.TypePostName, 100, 1);
 #end
#end
#end
                    hideLable();
                    break;
                case "del":
                    ${u.TabName}Entity sat = new ${u.TabName}Entity();
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK=="True")
                    sat.$c.ColumnName = eId;
#end
#end
                    sat.DataTable_Action_ = DataTable_Action.Delete;
                    if(BLLHelper.${u.TabName}InsertUpdateDelete(sat)>0)
                    {  
                        EventMessage.MessageBox(1, "操作成功", "删除记录ID:(" + eId + ")成功！", Icon_Type.OK, Utils.GetHomeBaseUrl("Default.aspx"));
                        break;

                    }
                    else
                    {
                        EventMessage.MessageBox(1, "操作失败", "ID:(" + eId + ")删除失败！", Icon_Type.Error, Utils.GetHomeBaseUrl("Default.aspx"));
                    } break;

            }
            hand.AddedEventHandler += new WebFormHandler.AddEventHandler(hand_AddedEventHandler);
            hand.ModifyedEventHandler+=new EventHandler(hand_ModifyedEventHandler);


        }
        #region 数据库操作触发事件
        protected void hand_AddedEventHandler(object sender,FrameWork.DBUtility.DbOperEventArgs e)
        {
            EventMessage.MessageBox(1, "操作成功", string.Format("({0}){1}成功!", Utils.GetCmdDirections(cmd),e.id.ToString()), Icon_Type.OK, Utils.GetHomeBaseUrl("Default.aspx"));
        }
        protected void hand_ModifyedEventHandler(object sender, EventArgs e)
        {
            EventMessage.MessageBox(1, "操作成功", string.Format("({0}){1}成功!", Utils.GetCmdDirections(cmd), eId.ToString()), Icon_Type.OK, Utils.GetHomeBaseUrl("Default.aspx"));
        }
        #endregion 

        #region 添加按钮
        /// <summary>
        /// 添加编辑按钮
        /// </summary>
        protected void addEditBtn()
        {
            HeadMenuButtonItem Bm0 = new HeadMenuButtonItem();
            Bm0.ButtonPopedom = PopedomType.Edit;
            Bm0.ButtonUrl = string.Format("?cmd=edit&eid={0}", eId); 
            HeadMenuWebControls1.ButtonList.Add(Bm0);
        }
        /// <summary>
        /// 添加删除按钮
        /// </summary>
        protected void addDelBtn()
        {
            HeadMenuButtonItem Bm0 = new HeadMenuButtonItem();
            Bm0.ButtonPopedom = PopedomType.Delete;
            Bm0.ButtonUrl = string.Format("DelData('?cmd=del&eid={0}')", eId);
            Bm0.ButtonUrlType = UrlType.JavaScript;
            HeadMenuWebControls1.ButtonList.Add(Bm0);
        }
        /// <summary>
        /// 添加新增按钮
        /// </summary>
        protected void addAddBtn()
        {
            HeadMenuButtonItem Bm0 = new HeadMenuButtonItem();
            Bm0.ButtonPopedom = PopedomType.Add;
            Bm0.ButtonUrl = string.Format("?cmd=add&eid={0}", eId);
            HeadMenuWebControls1.ButtonList.Add(Bm0);
        }

        #endregion 

        /// <summary>
        /// 隐藏编辑文本框和按钮
        /// </summary>
        protected void hideTextBox()
        {
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName!="bit")
            tb$c.ColumnName .Visible = false;
#end
#end
#end       
            btnSave.Visible = false;
            trBtn.Visible = false;
        }

        protected void hideLable()
        {
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName!="bit")
            lb$c.ColumnName .Visible = false;
#end
#end
#end 
             TopTr.Visible = false;
        }
#end
        protected void btnSaveClick(object sender, EventArgs e)
        {
            
        }
       
        protected bool  checkForm()
        {
           
            return true;
        }
    }
}

